//
//  DecorationViewFlowLayout.swift
//  EastNetWorkProject
//
//  Created by 伊伊 on 2023/12/20.
//

import UIKit
let background = "background"

protocol DecorationViewDelegateFlowLayout:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView:UICollectionView,backgroundForSectionAt section: NSInteger) -> UIColor
}
extension DecorationViewDelegateFlowLayout{
    func collectionView(_ collectionView:UICollectionView,backgroundForSectionAt section: NSInteger) -> UIColor{
        return UIColor.groupTableViewBackground
    }
}



class DecorationViewFlowLayout: UICollectionViewFlowLayout {
    var array:[UICollectionViewLayoutAttributes] =  [UICollectionViewLayoutAttributes]()
    override func prepare() {
        super.prepare()
     
        guard let numberOfSection = self.collectionView?.numberOfSections else { return  }
        for i in 0 ..< numberOfSection {
            let arrs = self.layoutAttributesForDecorationView(ofKind: background, at: IndexPath(item: 0, section: i))!
            self.array.append(arrs)
        }
    }
    override init() {
        super.init()
         self.register(DecorationView.self, forDecorationViewOfKind: background)
        }
 
    
    override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        let numberOfitem = self.collectionView!.numberOfItems(inSection: indexPath.section)
        let fistItem = self.layoutAttributesForItem(at: IndexPath(item: 0, section: indexPath.section))!
        
        let lastItem = self.layoutAttributesForItem(at: IndexPath(item: numberOfitem - 1, section: indexPath.section))!
        let  sectionInset = self.sectionInset
       let delegate = self.collectionView!.delegate as! UICollectionViewDelegateFlowLayout
        self.sectionInset = delegate.collectionView!(self.collectionView!, layout: self, insetForSectionAt: indexPath.section)
        var sectionFrame = CGRectUnion(fistItem.frame, lastItem.frame)
        if(self.scrollDirection == .vertical){
            sectionFrame.size.height = sectionFrame.size.height + sectionInset.top + sectionInset.bottom
            sectionFrame.size.width = self.collectionView!.frame.width
        }else{
            sectionFrame.size.width += sectionInset.left + sectionInset.right;
            sectionFrame.size.height = self.collectionView!.frame.size.height;
        }
       
       let arrs =  DecorationViewLayoutAttributes.init(forDecorationViewOfKind: background, with:indexPath)
         
        arrs.frame = sectionFrame
        arrs.zIndex = -1
        if let newDelegate  =  delegate as? DecorationViewDelegateFlowLayout{
            arrs.backgroundColor =  newDelegate.collectionView( self.collectionView!, backgroundForSectionAt: indexPath.section)
        }
        
        return arrs
    }
    

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var  array = super.layoutAttributesForElements(in: rect)!
        array.append(contentsOf: self.array)
        return array
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
